package org.alis.rpc.common.protocol;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * RpcProtocol desc:
 *
 * @author luochuan
 * @date 2020/9/21
 */
@Data
public class RpcProtocol implements Serializable {
    private static final long serialVersionUID = -1102180003395190700L;
    /**
     * service host
     */
    private String host;
    /**
     * service port
     */
    private int port;
    /**
     * service info list
     */
    private List<RpcServiceInfo> serviceInfoList;

    public String toJson() {
        return JSON.toJSONString(this);
    }

    public static RpcProtocol fromJson(String json) {
        return JSON.parseObject(json, RpcProtocol.class);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (Objects.isNull(o) || getClass() != o.getClass()) {
            return false;
        }
        RpcProtocol that = (RpcProtocol) o;
        return port == that.port &&
                Objects.equals(host, that.host) &&
                isListEquals(serviceInfoList, that.getServiceInfoList());
    }

    private boolean isListEquals(List<RpcServiceInfo> thisList, List<RpcServiceInfo> thatList) {
        if (thisList == null && thatList == null) {
            return true;
        }
        if (thisList == null || thatList == null || thisList.size() != thatList.size()) {
            return false;
        }
        return thisList.containsAll(thatList) && thatList.containsAll(thisList);
    }

    @Override
    public int hashCode() {
        return Objects.hash(host, port, serviceInfoList.hashCode());
    }

    @Override
    public String toString() {
        return toJson();
    }


}